//
//  BGAddInviteOneTableViewCell.m
//  bigMall
//
//  Created by LTOVE on 16/8/23.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGAddInviteOneTableViewCell.h"

#import "BGAddInviteOneTextFildCellModel.h"
#import "BGAddInviteOnePickViewModel.h"
#import "BGAddInviteOneTextAreaModel.h"
#import "BGAddInviteOneTitleModel.h"
#import "BGAddInviteOneFenQiKuanModel.h"
#import "BGSmallNumModel.h"

#import "BGInitArea.h"

#define textAreaHeigh 60
#define LineMagin 10
static NSString *const reuseIdentify = @"cell";

@interface BGAddInviteOneTableViewCell ()<UITextViewDelegate,UITextFieldDelegate,BGAddInviteOnePickViewModelDeleGate>

@end

@implementation BGAddInviteOneTableViewCell

-(void)pickViewChangedIndexValues:(id)indexValue
{
//    NSLog(@"%@",self.model.indexPath);
    BGAddInviteOnePickViewModel *itemModel = (BGAddInviteOnePickViewModel *)self.model;
    if (itemModel.isAddrPick) {
        itemModel.addrIdStrArray = [BGInitArea idstrWithIndexArray:indexValue];
        self.model = itemModel;
    }else{
        itemModel.indexArray = indexValue;
    }
    [self setDeleGatess];
    
}

- (void)setDeleGatess
{
    if ([_delegate respondsToSelector:@selector(cellModelChangedWithModel:)]) {
        [_delegate cellModelChangedWithModel:self.model];
    }
}

- (void)pickViewChangedValues:(id)value
{
//    NSLog(@"----%@",value);
    NSString *title = [self.model.title substringToIndex:2];
    if ([title isEqualToString:@"分类"]) {
        NSMutableString *str = [NSMutableString string];
//        for (NSString *strs in value) {
//            [str appendString:strs];
//        }
        str = [value lastObject];
        NSMutableString *titleStr = [NSMutableString stringWithFormat:@"%@",self.model.title];
        NSRange rang = [titleStr rangeOfString:@":"];
        titleStr = [NSMutableString stringWithFormat:@"%@",[titleStr substringToIndex:rang.location + 1]];
        [titleStr appendString:[NSString stringWithFormat:@" %@",str]];
        self.model.title = titleStr;
    }else{
    
    if ([value isKindOfClass:[NSDate class]]) {
        NSDate *date = value;
        NSTimeInterval time = [date timeIntervalSince1970];
        NSString *timestr = [NSString stringWithFormat:@"%f",time];
        timestr = [timestr substringToIndex:10];
        
        NSDateFormatter *format = [[NSDateFormatter alloc]init];
        [format setDateFormat:@"yyy-MM-dd"];
        NSString *timeText = [format stringFromDate:date];
        
        NSMutableString *titleStr = [NSMutableString stringWithFormat:@"%@",self.model.title];
        NSRange rang = [titleStr rangeOfString:@":"];
        titleStr = [NSMutableString stringWithFormat:@"%@",[titleStr substringToIndex:rang.location + 1]];
        [titleStr appendString:[NSString stringWithFormat:@" %@",timeText]];
        
        
        self.model.title = titleStr;
        
        self.model.timeStr = timestr;
    }else{
        NSMutableString *str = [NSMutableString string];
        for (NSString *strs in value) {
            [str appendString:strs];
        }
        NSMutableString *titleStr = [NSMutableString stringWithFormat:@"%@",self.model.title];
        NSRange rang = [titleStr rangeOfString:@":"];
        titleStr = [NSMutableString stringWithFormat:@"%@",[titleStr substringToIndex:rang.location + 1]];
        [titleStr appendString:[NSString stringWithFormat:@" %@",str]];
        self.model.changedStr = str;
        self.model.title = titleStr;
    }

    }
    [self setDeleGatess];
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    GWLog(@"ssssssssssssssssssssssssssssssssssss");
    [self setWillBeginEditing];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self setWillBeginEditing];
}

- (void)setWillBeginEditing
{
    if ([_delegate respondsToSelector:@selector(cellWillBeginEditingWithModel:)]) {
        [_delegate cellWillBeginEditingWithModel:self.model];
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    BGAddInviteOneTextAreaModel *itemModel = (BGAddInviteOneTextAreaModel *)self.model;
    itemModel.textViewText = textView.text;
    itemModel.changedStr = textView.text;
    self.model = itemModel;
    [self setDeleGatess];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
//    NSLog(@"%@",[self.model class]);
//    BGAddInviteOneTextFildCellModel *itemModel = (BGAddInviteOneTextFildCellModel *)self.model;
//    itemModel.textFildStr = textField.text;
    NSLog(@"%ld",(long)textField.tag);
    if (textField.tag) {
        NSLog(@"%@",textField.text);
        
        switch (textField.tag) {
            case 1:
                self.model.firstMoney = textField.text;
                
                break;
            case 2:
                self.model.SecMoney = textField.text;
                
                break;
            case 3:
                self.model.LastMoney = textField.text;
                
                break;
            default:
                break;
        }
        
    }else{
    self.model.textFildStr = textField.text;
        self.model.changedStr = textField.text;
    
    }
    [self setDeleGatess];
}
- (void)setModel:(BGAddInviteOneBaseModel *)model
{
    _model = model;
    //添加所有子控制器
    [self setUpAllSubviewsWithModel:model];
}
/**
 *  添加所有子控制器
 *
 *  @param model model description
 */
- (void)setUpAllSubviewsWithModel:(BGAddInviteOneBaseModel *)model
{
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    self.textLabel.text = nil;
    if (model.isShowAccessoryView) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if ([model isKindOfClass:[BGAddInviteOneTextFildCellModel class]]) {
        //单行cell 可填写

        BGAddInviteOneTextFildCellModel *itemModel = (BGAddInviteOneTextFildCellModel *)model;
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 44)];
        UILabel *titleLabel = [self setUpLabelWithTitle:model.title];
        titleLabel.centerY = view.height * 0.5;
        [view addSubview:titleLabel];
        UITextField *textFild = [UITextField new];
        textFild.width = screenW - 2 * BGMaginLeft - titleLabel.width;
        textFild.y = titleLabel.y;
        textFild.x = CGRectGetMaxX(titleLabel.frame) + 5;
        textFild.height = titleLabel.height;
//        textFild.backgroundColor = read_;
        textFild.placeholder = @"请填写";
        textFild.font = Font30;
        NSString *title = [model.title substringToIndex:2];
        if ([title isEqualToString:@"单价"] ||[title isEqualToString:@"数量"] ||[title isEqualToString:@"投标"] ||[title isEqualToString:@"最小"]) {
            textFild.keyboardType = UIKeyboardTypeDecimalPad;
            
        }else{
            textFild.keyboardType = UIKeyboardTypeDefault;
        }
        if (itemModel.textFildStr.length) {
            textFild.text = itemModel.textFildStr;
        }
        textFild.delegate = self;
        [view addSubview:textFild];
       
        for (UIView *view in self.contentView.subviews) {
            [view removeFromSuperview];
        }
        [self.contentView addSubview:view];
        
    }else if ([model isKindOfClass:[BGAddInviteOnePickViewModel class]] || [model isKindOfClass:[BGAddInviteOneTitleModel class]]){
        //弹窗 pickView //展示  标题  不交互
        if ([model isKindOfClass:[BGAddInviteOnePickViewModel class]]) {
            BGAddInviteOnePickViewModel *modes = (BGAddInviteOnePickViewModel *)model;
            modes.delegate = self;
        }
        self.textLabel.attributedText = [self attrStrWithStr:model.title withColor:textGrayColor];
        
    }else if ([model isKindOfClass:[BGAddInviteOneTextAreaModel class]]){
        //文本域
        BGAddInviteOneTextAreaModel *itemModel = (BGAddInviteOneTextAreaModel *)model;
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, textAreaHeigh)];
        UILabel *titleLabel = [self setUpLabelWithTitle:model.title];
        [view addSubview:titleLabel];
        UITextView *textView = [[UITextView alloc]init];
        textView.width = screenW - titleLabel.width - 2 * BGMaginLeft - 5;
        textView.height = view.height - 2 * LineMagin;
        textView.y = titleLabel.y - 5;
        textView.x = CGRectGetMaxX(titleLabel.frame) + 5;
//        textView.backgroundColor = read_;
        textView.delegate = self;
        textView.text = itemModel.textViewText;
        [view addSubview:textView];
        for (UIView *view in self.contentView.subviews) {
            [view removeFromSuperview];
        }
        [self.contentView addSubview:view];
    }else if ([model isKindOfClass:[BGAddInviteOneFenQiKuanModel class]]){
    
        //分期款视图
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 44)];
        UILabel *firstLabel = [self setUpLabelWithTitle:@"定金"];
        UILabel *secLabel = [self setUpLabelWithTitle:@"中期款"];
        UILabel *lastLabel = [self setUpLabelWithTitle:@"尾款"];
        UITextField *firstTextFild = [self setUptextFildWithTag:1];
        UITextField *sectextFild = [self setUptextFildWithTag:2];
        UITextField *lastTextFild = [self setUptextFildWithTag:3];
        CGFloat textFildWidth = (screenW - 2 * BGMaginLeft - 25 - firstLabel.width - secLabel.width - lastLabel.width) / 3;
        firstTextFild.width = sectextFild.width = lastTextFild.width = textFildWidth;
        firstLabel.x = BGMaginLeft;
        firstLabel.centerY = view.height * 0.5;
        firstTextFild.x = CGRectGetMaxX(firstLabel.frame) + 5;
        firstTextFild.centerY = firstLabel.centerY;
        secLabel.x = CGRectGetMaxX(firstTextFild.frame) + 5;
        secLabel.centerY = firstLabel.centerY;
        sectextFild.x = CGRectGetMaxX(secLabel.frame) + 5;
        sectextFild.centerY = firstLabel.centerY;
        lastLabel.x = CGRectGetMaxX(sectextFild.frame) + 5;
        lastLabel.centerY = firstLabel.centerY;
        lastTextFild.x = CGRectGetMaxX(lastLabel.frame) + 5;
        lastTextFild.centerY = firstLabel.centerY;
        firstTextFild.text = model.firstMoney;
        sectextFild.text = model.SecMoney;
        lastTextFild.text = model.LastMoney;
        [view addSubview:firstLabel];
        [view addSubview:firstTextFild];
        [view addSubview:secLabel];
        [view addSubview:sectextFild];
        [view addSubview:lastLabel];
        [view addSubview:lastTextFild];
        for (UIView *view in self.contentView.subviews) {
            [view removeFromSuperview];
        }
        [self.contentView addSubview:view];
    }else if ([model isKindOfClass:[BGSmallNumModel class]]){
        //显示信誉值或者保证金
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 88)];
        UILabel *titleLabel = [self setUpLabelWithTitle:model.title];
        titleLabel.centerY = view.height * 0.5;
        UILabel *secTopLabel = [UILabel new];
        secTopLabel.attributedText = [self attrStrWithStr:model.OnwValue withColor:read_];
        UILabel *secBottomLabel = [UILabel new];
        secBottomLabel.attributedText  = [self attrStrWithStr:model.needValue withColor:read_];
        secTopLabel.x = CGRectGetMaxX(titleLabel.frame) + 5;
        secBottomLabel.x = secTopLabel.x;
        secTopLabel.y = LineMagin;
        secBottomLabel.y = view.height * 0.5 +LineMagin;
        [secTopLabel sizeToFit];
        [secBottomLabel sizeToFit];
        [view addSubview:titleLabel];
        [view addSubview:secTopLabel];
        [view addSubview:secBottomLabel];
        [self.contentView addSubview:view];
    }
//    else if ([model isKindOfClass:[BGAddInviteOneTitleModel class]]){
//        
//        self.textLabel.attributedText = [self attrStrWithStr:model.title];
//    }
}

- (UITextField *)setUptextFildWithTag:(NSInteger)tag
{
    UITextField *textFild = [UITextField new];
    textFild.placeholder = @"请填写";
    textFild.height = 33;
//    textFild.width = 44;
    textFild.keyboardType = UIKeyboardTypeDecimalPad;
    textFild.delegate = self;
    textFild.tag = tag;
    return textFild;
}

- (UILabel *)setUpLabelWithTitle:(NSString *)title
{
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.textColor = textBlackColor;
    titleLabel.font = Font34;
    [titleLabel sizeToFit];
    titleLabel.x = 16;
    titleLabel.y = LineMagin;
    return titleLabel;
}

- (NSMutableAttributedString *)attrStrWithStr:(NSString *)str withColor:(UIColor *)color
{
    NSDictionary *dic = @{
                          NSFontAttributeName:Font34,
                          NSForegroundColorAttributeName:textBlackColor
                          };
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:str attributes:dic];
    NSRange rang = [str rangeOfString:@":"];
    NSDictionary *secDic = @{
                             NSFontAttributeName:Font30,
                             NSForegroundColorAttributeName:color
                             };
    [attr addAttributes:secDic range:NSMakeRange(rang.location + 1, str.length -1 - rang.location)];
    return attr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
//    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // Configure the view for the selected state
}


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    BGAddInviteOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify];
    }
    return cell;
    
}

@end
